/*
 * show.js
 */

$(function(){
    var start = 0;
    var limit = 20;
    var loading = false;
    var end = false;
    var more = $('#more');
    var enable = 1;

    function getList(pageStart, pageSize){
        more.text('加载中');
        loading = true;
        $.ajax({
            url: config.api.lists,
            data: {
                start: pageStart || start,
                limit: pageSize || limit,
                enable: enable
            }
        }).then(function(ret){
            loading = false;
            more.text('查看更多');
            if(ret.data.length === 0){
                end = true;
                more.text('没有更多了');
                return;
            }
            console.log(ret);

            if(enable == 1){
                var html = Mustache.render($('#tmpl-list').html(), {list: ret.data});
                $('#lists-enable').append($(html));
            }else{
                var html = Mustache.render($('#tmpl-list-recover').html(), {list: ret.data});
                $('#lists-disable').append($(html));
            }

            start = start + (pageSize || limit);
        }, function(err){
            console.log(err);
            if(err.readyState === 4 && JSON.parse(err.responseText).status === 403){
                showLogin();
            }
            return;

        })
    }

    function getListByMobile(mobile){
        console.log(mobile)
        more.text('加载中');
        loading = true;
        $.ajax({
            url: config.api.listsByMobile + mobile,
            data: {
                activityId: 1
            }
        }).then(function(ret){
            $('#header li').removeClass('active');
            $('#header li').eq(0).addClass('active');
            enable = $('#header li').eq(0).data('enable');
            $('#lists-enable').show();
            $('#lists-disable').hide();
            $('#lists-enable li').remove();
            $('#lists-disable li').remove();
            start = 0;
            loading = false;
            if(!ret.data){
                //end = true;
                more.text(ret.message || '未找到');
                return;
            }else{
                more.hide();
            }
            console.log(ret);

            var html = Mustache.render($('#tmpl-list').html(), {list: [ret.data]});

            $('#lists-enable').html($(html));
        }, function(err){
            console.log(err);
            loading = false;
            return;

        })
    }
    function login(user, pwd){
        more.text('加载中');
        loading = true;
        $.ajax({
            url: config.base_api + '/api/rest/login/admin',
            data: {
                name: user,
                verify: pwd
            }
        }).then(function(ret){
            console.log('login success', ret)
            showList();
        }, function(err){
            console.log(err);
            if(err.readyState === 4 && JSON.parse(err.responseText).status === 403){
                location.href = 'login.html'
            }
            return;
        })
    }
    function showList(){
        getList();
        $('.login').hide();
        $('.main').show();
    }
    function showlogin(){
        $('.login').show();
        $('.main').hide();
        location.href = 'login.html';
    }

    function dialog(msg){
        $('.dialog .dialog-cnt').text(msg);
        $('.dialog').show();
    }
    $('.dialog .dialog-ft').on('click', function(e){
        $('.dialog').hide();
    });

    more.on('click', function(e){
        if(loading || end){
            return false;
        }
        getList();
        return false;
    })

    // search
    $('#search').on('click', function(e){
        var mobile = $('input[name="search"]').val();
        console.log('search', mobile);
        if(!utils.mobile(mobile)){
            dialog('请输入正确的手机号码'); 
        }
        if(loading || end){
            return false;
        }
        getListByMobile(mobile);
        return false;
    })


    $('.login button').on('click', function(e){
        var username = $('input[name=username]').val();
        var password = $('input[name=password]').val();
        if(!username || !password){
            alert('请输入用户名和密码')
            return;
        }
        login(username, password);
    })

    $('#header li').on('click', function(e){
        if($(this).hasClass('active')){
            return;
        }
        $('#header li').removeClass('active');
        $(this).addClass('active');
        enable = $(this).data('enable');
        console.log('enable', enable);
        if(enable == 1){
            $('#lists-enable').show();
            $('#lists-disable').hide();
            $('#lists-disable li').remove();
        }else{
            $('#lists-enable').hide();
            $('#lists-disable').show();
            $('#lists-enable li').remove();
        }
        start = 0;
        loading = false;
        more.text('查看更多');
        end = false;
        getList();
    })

    $('.main').delegate('.btn-deny', 'click', function(e){
        console.log('btn-deny');
        var target = $(this);
        var id = $(this).closest('li').data('id');
        console.log(id);

        if(confirm('确认拒绝该作品？')){
            deny(id, target);
        }
    })

    $('.main').delegate('.btn-recover', 'click', function(e){
        console.log('btn-recover');
        var target = $(this);
        var id = $(this).closest('li').data('id');
        console.log(id);

        if(confirm('确认通过该作品？')){
            recover(id, target);
        }

    })

    function deny(id, target){
        $.ajax({
            url: config.api.deny + id
        }).then(function(ret){
            console.log('deny', ret);
            target.text('通过');
            target.removeClass('btn-deny').addClass('btn-recover');
            target.closest('li').remove();
        }, function(err){
            console.log(err);
            alert('未登录')
            if(err.readyState === 4 && JSON.parse(err.responseText).status === 403){
                location.href = 'login.html';
            }
        })
    }

    function recover(id, target){
        $.ajax({
            url: config.api.recover + id
        }).then(function(ret){
            console.log('recover', ret);
            target.text('拒绝');
            target.removeClass('btn-recover').addClass('btn-deny');
            target.closest('li').remove();
        }, function(err){
            console.log(err);
            alert('未登录')
            if(err.readyState === 4 && JSON.parse(err.responseText).status === 403){
                location.href = 'login.html';
            }
        })
    }




    getList();
})
